package ee.ut.cs.mc.l2;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * @author Kaarel Jõgeva
 */
public class Converter extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_converter);

        Button btn = (Button) findViewById(R.id.convertBtn);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                EditText tempFieldC = (EditText) findViewById(R.id.temperatureTextC);
                EditText tempFieldF = (EditText) findViewById(R.id.temperatureTextF);
                String stringC = tempFieldC.getText().toString();
                String stringF = tempFieldF.getText().toString();
                if (!stringC.isEmpty()) {
                    float tempValueF = Float.parseFloat(stringC) * 9 / 5 + 32;
                    tempFieldF.setText(tempValueF + "");
                } else {
                    float tempValueC = (Float.parseFloat(stringF) - 32) * 5 / 9;
                    tempFieldC.setText(tempValueC + "");
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_converter, menu);
        return true;
    }
}
